# -*- coding: utf-8 -*-
"""
Created on Fri Sep  6 09:37:01 2013

@author: chico
"""

from msTreeMapperWidget import *

  
class WidgetsContainer(QtGui.QWidget):
    
    def __init__(self,mainwin):
        super(WidgetsContainer, self).__init__()
        self.MainWindowContainer = mainwin
        
        #self.tabWidget = QtGui.QTabWidget()
        #self.splitter = QtGui.QSplitter()
        
        #self.tabWidget.setSizePolicy(QtGui.QSizePolicy.Expanding,QtGui.QSizePolicy.Expanding)
        
        self.inspectorGroup=QtGui.QGroupBox()  
        self.vbox =  QtGui.QVBoxLayout()
        self.inspectorGroup.setLayout(self.vbox)   
        
        self.scrollInspectorArea = QtGui.QScrollArea()
        self.scrollInspectorArea.setWidget(self.inspectorGroup)
        
        self.visualizorGroup=QtGui.QGroupBox()  
        self.vboxVisu =  QtGui.QVBoxLayout()
        self.visualizorGroup.setLayout(self.vboxVisu)   
        
        self.scrollVisuArea = QtGui.QScrollArea()
        self.scrollVisuArea.setWidget(self.visualizorGroup)
        
        #self.scriptWidget=uiScriptWidget(self.MainWindowContainer)
        
        #self.splitter.addWidget(self.scriptWidget)
        #self.tabWidget.addTab(self.scriptWidget,"Script")    
        """        
        self.webView=QtWebKit.QWebView()
        self.tabWidget.addTab(self.webView,"Documentation")
        self.webView.load("http://atomism.googlecode.com/svn/trunk/doc/html/index.html")
        
        self.tabWidget.currentChanged.connect(self.changeTab)
        """
        hbox = QtGui.QHBoxLayout()
        #hbox.addWidget(self.tabWidget,4)
        hbox.addWidget(self.scrollVisuArea)
        self.setLayout(hbox)    
        
        self.setWindowTitle('Buttons')    
        self.widgets=[]
    
    def changeTab(self,i):
        
        if(i==0):
            self.MainWindowContainer.createMenus()
        if(i==1):
            self.MainWindowContainer.createMenusScript()
             
        
    def update(self,myobject):

        # Update the documentation
        doxy=self.getDoxygenName(myobject.getType())
        last=[]
        last=doxy.split(':')
        self.MainWindowContainer.webView.load("http://atomism.googlecode.com/svn/trunk/doc/html/classatomism_1_1"+last[-2]+".html#details")
        
        # Load the widgets
        types = myobject.getType().split(':')
        width=0
        height=0
        #splitter = QtGui.QSplitter()
        #splitter.setOrientation(QtCore.Qt.Vertical)
        #self.vbox.addWidget(splitter)
        print "update"
        for cls in msWidget.__subclasses__():
            print cls
            
        for thetype in types[0:-1]:
            widget=0
            thetype = 'ui'+thetype[2:]
            try:
                widget = globals()[thetype](myobject,self.MainWindowContainer)
                width  = max(width,widget.size().width())
                height = height + widget.size().height()
            except:
                self.MainWindowContainer.reportException(sys)
            if widget:
                self.widgets.append(widget)     
                
        
        a=100
        self.inspectorGroup.setMinimumSize(width+a,height+a)
        self.inspectorGroup.setMaximumSize(width+a,height+a)
        
        for widget in self.widgets:
            self.vbox.addWidget( widget, QtCore.Qt.AlignLeft )  
            #splitter.addWidget( widget)  
        
            
    def getDoxygenName(self,name):
        s1 = re.sub('(.)([A-Z][a-z]+)',r'\1_\2',name)
        return re.sub('([a-z0-9])([A-Z])',r'\1_\2',s1).lower()
